package com.ztgf.logistics.service.deppon;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.vo.UserVO;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsUserDTO;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserService;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.logistics.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.logistics.mapper.deppon.LogisticsBillMapper;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillService;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillSaveBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillDTO;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物流账单表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:14:02
 */
@DubboService
@Slf4j
public class LogisticsBillServiceImpl extends ServiceImpl<LogisticsBillMapper, LogisticsBillDTO> implements ILogisticsBillService{

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ILogisticsUserService logisticsUserService;

    @Override
    public boolean add(LogisticsBillSaveBO saveBO) {
        LogisticsBillDTO dto = new LogisticsBillDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public Result<Page<LogisticsBillDTO>> queryPageList(LogisticsBillBO bo) {
        Page<LogisticsBillDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        Wrapper<LogisticsBillDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        if(bo.getBillPlatId() != null){
            wrapper.eq("bill_plat_id", bo.getBillPlatId());
        }
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("id", false);

        page = this.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @Override
    public LogisticsBillDTO getNoPayBill(Long id) {
        Wrapper<LogisticsBillDTO> wrapper = new EntityWrapper<>();
        wrapper.in("pay_status", "0,1,3");
        wrapper.eq("shop_id", id);
        wrapper.orderBy("id", false);
        wrapper.last("limit 1");
        LogisticsBillDTO dto = this.selectOne(wrapper);
        return dto;
    }

    /**
     * 处理逾期
     * @param billDTO
     */
    @Override
    @Transactional
    public void saveOverdue(LogisticsBillDTO billDTO) {
        LogisticsUserDTO userDTO = logisticsUserService.selectById(billDTO.getShopId());
        if(userDTO == null){
            log.error("账单用户错误"+ JSON.toJSONString(billDTO));
            return;
        }
        LogisticsUserDTO newUserDTO = new LogisticsUserDTO();
        newUserDTO.setOverdueTimes(userDTO.getOverdueTimes() + 1);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id", userDTO.getId());
        wrapper.eq("overdue_times", userDTO.getOverdueTimes());
        if(!logisticsUserService.update(newUserDTO, wrapper)){
            throw new ServiceException(CodeEnum.FAIL_100008);
        }
        LogisticsBillDTO newBillDTO = new LogisticsBillDTO();
        newBillDTO.setOverdueStatus(1);
        wrapper = new EntityWrapper();
        wrapper.eq("id", billDTO.getId());
        wrapper.eq("overdue_status", 0);
        if(!this.update(newBillDTO, wrapper)){
            throw new ServiceException(CodeEnum.FAIL_100008);
        }
    }
}
